<?php

class Bizprofile_Widgets_Partner_Widget extends Tomato_Widget 
{
	protected function _prepareShow() 
	{
		$enterprise_id = 0;
		$user_id = 0;
		$auth = Zend_Auth::getInstance();
		$enterprise = null;
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$lang = Zend_Controller_Front::getInstance()->getRequest()->getParam('lang');
		$this->_view->assign('lang', $lang);
		if ($auth->hasIdentity()) {
			$user = $auth->getIdentity();
			if($user->enterprise){
				$enterprise_id = $user->enterprise->id;
				$user_id = $user->user_id;
				$enterprise = $user->enterprise;
			}
		}
		else
			$this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
		
		$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		if($params['module'] == 'bizprofile' && $params['controller'] == 'Enterprise' && $params['action'] == 'index' && isset($params['enterprise_id'])){
			$enterprise_id = $params['enterprise_id'];				
			$enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
			$enterpriseDao->setDbConnection($conn);
			$enterprise = $enterpriseDao->getById($params['enterprise_id']);		
		}
		
		if($enterprise != null && $enterprise_id > 0 && $enterprise->frontend_active == 1){
			$this->_view->assign('enterprise', $enterprise);
			
			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			$this->_view->assign('user_entity', $user_entity);
			
			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
			if($permission != null){
				$permission->enterprise_info = unserialize($permission->enterprise_info);			
				$this->_view->assign('permission', $permission);
			}	
			
			$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule("bizprofile")->getPartnerDao();
			$partnerDao->setDbConnection($conn);
			
			if($user_entity == 0 ){
				$notify = $partnerDao->getCountNotify($user->enterprise->id);
				if ($notify != null) $this->_view->assign('numNotify', count($notify));
			}
			
			$status = "none";
			if($user_entity >= 3 ){
				$status = "none";
				
				$partner = $partnerDao->getPartner($user->enterprise->id, $enterprise->id);
				
				if ($partner != null) {
					$status = $partner->partner_status;
				}
				
				$partner_to = $partnerDao->getPartner($enterprise->id, $user->enterprise->id);
				
				if ($partner_to != null){
					$status = $partner_to->partner_status;
					if ($status == 'pending' || $partner_to->partner_status == 'pending'){
						$status = "confirm";
					}
				}
				$this->_view->assign('status', $status);
				
				if ($partner != null)
					$this->_view->assign('partner', $partner);
				if ($partner_to != null)
					$this->_view->assign('partner_to', $partner_to);
				
				$this->_view->assign('enterprise_id', $user->enterprise->id);
				$this->_view->assign('relation_id', $enterprise->id);
				
				$purl = $params['module'] . "_" . strtolower($params['controller']) . "_" . str_replace("-","_",$params['action']);
				$this->_view->assign('purl', $purl);
			}
		}	
	}
	
	protected function _prepareConfig()
	{
		
	}
}
